package com.ruoyi.hxf.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.hxf.domain.Education;
import com.ruoyi.hxf.domain.Topic;
import com.ruoyi.hxf.mapper.TopicMapper;
import com.ruoyi.hxf.service.ITopicService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collection;
import java.util.Map;

/**
 * 帖子创建管理Service业务层处理
 *
 * @author hxf
 * @date 2023-1-18
 */
@RequiredArgsConstructor
@Service
public class TopicServiceImpl implements ITopicService {
    private final TopicMapper topicMapper;
    private QueryWrapper<Topic> wrapper;

    /**
     * 查询帖子管理
     */
    @Override
    public Topic queryById(String topicId) {
        return topicMapper.selectTopicById(topicId);
    }

    /**
     * 查询帖子管理列表
     */
    @Override
    public TableDataInfo<Topic> selectPageTopicList(Topic topic, PageQuery pageQuery) {
        Page<Topic> page = topicMapper.selectPageAndUser(pageQuery.build(), this.buildQueryWrapper(topic));
        return TableDataInfo.build(page);
    }


    private Wrapper<Topic> buildQueryWrapper(Topic topic) {
        wrapper = Wrappers.query();
        Map<String, Object> params = topic.getParams();
        wrapper.like(StringUtils.isNotBlank(topic.getTopicTitle()), "ft.topic_title", topic.getTopicTitle())
            .eq("ft.is_delete", 0)
            .eq(StringUtils.isNotBlank(topic.getTopicStatus()), "ft.topic_status", topic.getTopicStatus())
            .between(params.get("beginCreateTime") != null && params.get("endCreateTime") != null,
                "ft.create_time", params.get("beginCreateTime"), params.get("endCreateTime"))
            .last("order by create_time desc");
        return wrapper;
    }

    /**
     * 新增帖子信息
     *
     * @param topic 帖子信息
     * @return 结果
     */
    @Override
    public int insertTopic(Topic topic) {
        topic.setCreateBy(LoginHelper.getUsername());
        topic.setCreateTime(DateUtils.getNowDate());
        return topicMapper.insertTopic(topic);
    }

    /**
     * 修改帖子管理
     */
    @Override
    public Boolean updateByBo(Topic topic) {
        validEntityBeforeSave(topic);
        return topicMapper.updateById(topic) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Topic entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除帖子管理
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
            for (String id : ids) {
                Topic topic = topicMapper.selectById(id);
                topic.setIsDelete("1");
                topic.setUpdateBy(String.valueOf(LoginHelper.getUserId()));
                topicMapper.updateById(topic);
            }
        }
        return true;
    }

    @Override
    public int updateByTopic(Topic topic) {
        return topicMapper.updateByTopic(topic);
    }

    @Override
    public TableDataInfo<Topic> selectAppPageTopicList(Topic topic, PageQuery pageQuery) {
        wrapper = Wrappers.query();
        Page<Topic> page = topicMapper.selectPageAndUser(pageQuery.build(),
            wrapper
                .eq("ft.is_delete", 0)
                .eq("ft.topic_status", "1")
                .last("order by update_time desc"));
        return TableDataInfo.build(page);
    }
    @Override
    public TableDataInfo<Topic> selectMyAppPageTopicList(Topic topic, PageQuery pageQuery) {
        wrapper = Wrappers.query();
        Page<Topic> page = topicMapper.selectPageAndUser(pageQuery.build(),
            wrapper
                .eq(StringUtils.isNotBlank(topic.getCreateBy()),"ft.create_by",topic.getCreateBy())
                .eq("ft.is_delete", 0)
                .last("order by create_time desc"));
        return TableDataInfo.build(page);
    }

    @Override
    public int updateTopicByTopicId(Topic topic) {
        return topicMapper.updateTopicByTopicId(topic);
    }

    @Override
    public String  selectCollectionByUserIdAndTopicId(Long userId, Long topicId,String type) {
        return topicMapper.selectCollectionByUserIdAndTopicId(userId,topicId,type);
    }

    @Override
    public int insertTopicLike(String topicId,String userId, String status,String type) {
        return topicMapper.insertTopicLike(topicId,userId,status,type);
    }

    @Override
    public boolean selectTopicLike(String topicId, String userId,String type) {
        return topicMapper.selectTopicLike(topicId,userId,type);
    }

    @Override
    public void updateTopicLike(String topicId, String userId, String status,String type) {
         topicMapper.updateTopicLike(topicId,userId,status,type);
    }
}
